<?php
/**
 * Created by PhpStorm.
 * DateTime: 2020/11/3 9:06
 * Intro:
 */

namespace addons\podunk\model;


use app\common\library\Auth;
use think\Model;

class Device extends Model
{
    protected $name = 'podunk_device';

    protected $autoWriteTimestamp = 'int';

    protected $createTime = 'createtime';

    protected $updateTime = '';

    protected $append = [
    ];

    public static function getDeviceList($search = null)
    {
        if($search){
            $where = [
                'name'=>['LIKE',"%".$search."%"],
            ];
        }else{
            $where = [];
        }

        $deviceList = self::with('deviceGroup')->where(['user_id'=>Auth::instance()->id])->where(function($query) use($where){
            $query->whereOr($where);
        })->order('id desc')->paginate(10);

        if($deviceList){
            return $deviceList;
        }
        return null;
    }

    public function deviceGroup()
    {
        return $this->belongsTo('DeviceGroup','group_id','id');
    }
}